package com.tongda.menu.adapter;

import java.util.HashMap;
import java.util.List;

import com.tongda.core.vo.CancelDishReason;
import com.tongda.menu.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;


public class TuicaiAdapter extends BaseAdapter{
	private List<CancelDishReason> reasons;
	private Context context;
	private int selection=-1;
	 HashMap<String,Boolean> states=new HashMap<String,Boolean>();
	
	public List<CancelDishReason> getReasons() {
		return reasons;
	}

	public void setReasons(List<CancelDishReason> reasons) {
		this.reasons = reasons;
	}

	public TuicaiAdapter(Context context,List<CancelDishReason> reasons){
		this.reasons = reasons;
		this.context = context;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return reasons!=null?reasons.size():0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if ((reasons != null && reasons.size() > 0) && (position >= 0 && position < reasons.size())) {
			return reasons.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.dialogview_item, null);
			holder = new ViewHolder();
			holder.reasonName = (TextView) convertView.findViewById((R.id.dialogview_text));
			holder.radioButton=(RadioButton) convertView.findViewById(R.id.dialogview_radio_btn);
			convertView.setTag(holder);//绑定ViewHolder对象                   		 
		}else{
			holder = (ViewHolder)convertView.getTag();//取出ViewHolder对象    
		}
		
		final CancelDishReason cdr = reasons.get(position);
		holder.reasonName.setText(cdr.getName());
       //当RadioButton被选中时，将其状态记录进States中，并更新其他RadioButton的状态使它们不被选中
		
//		boolean res=false;
//           holder.radioButton.setOnClickListener(new OnClickListener() {
// 			
// 			@Override
// 			public void onClick(View v) {
// 				for(String key:states.keySet()){
// 					states.put(key, false);
// 				}
// 				states.put(String.valueOf(position), radioBut.isChecked());
// 				TuicaiAdapter.this.notifyDataSetChanged();
// 		}
// 		});
//           if(states.get(String.valueOf(position))==null){
//        	   states.put(String.valueOf(0), true);
//        	   states.put(String.valueOf(position), false);
//           }else if(states.get(String.valueOf(position))==false){
//        	   res=false;
//        	   states.put(String.valueOf(position), false);
//           }else
//        	   res=true;
//           holder.radioButton.setChecked(res);
		
		if(selection==position){
			holder.radioButton.setChecked(true);
		}else{
			holder.radioButton.setChecked(false);
		}
         return convertView;
     }

	public int getSelection() {
		return selection;
	}

	public void setSelection(int selection) {
		this.selection = selection;
	}
	
	
}


/**存放控件*/

class ViewHolder{
	TextView reasonName;
	RadioButton radioButton;
}

